package com.laxcen.basiclib.utils;

import android.app.Activity;
import java.util.Stack;

/**
 * Create by 游德禄 on 2019-12-5
 */
public class ActivityCollectorUtil {

    private static Stack<Activity> actList =new Stack<Activity>();
    public static void addActivity(Activity act) {
        if(act.getClass().getName().endsWith(".WebActivity")){
            //web页面不处理
            return;
        }
        if(hasActivity(act.getClass())!=-1){
           act.finish();
        }else{
            actList.add(act);
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public static Activity currentActivity(){
        Activity activity=actList.lastElement();
        return activity;
    }

    public static void removeActivity(Activity act) {
        if(act.getClass().getName().endsWith(".WebActivity")){
            //web页面不处理
            return;
        }
        actList.remove(act);
    }

    //判断某个activity是否已打开
    public static int hasActivity(Class cls){
        for (Activity act : actList) {
            if (act.getClass().getName().equals(cls.getName())) {
               return act.getTaskId();
            }
        }
        return -1;
    }

    public static void finish(int taskId){
        for (Activity act : actList) {
            //NLog.e("gsdgsdhsd",act.getTaskId(),taskId);
            if (act.getTaskId()==taskId) {
                act.finish();
            }
        }
    }
    public static void finish(Class cls){
        for (Activity act : actList) {
            if (act.getClass().getName().equals(cls.getName())) {
                act.finish();
            }
        }
    }
    public static void finishAll() {
        for (Activity act : actList) {
            if (!act.isFinishing()) {
                act.finish();
            }
        }
    }
}
